<?php
define('FPDF_FONTPATH',dirname(__FILE__).'/../fpdf/font/');
require(dirname(__FILE__).'/../fpdf/fpdf.php');

class PDF extends FPDF
{

// Load data
function LoadData($file)
{
    // Read file lines
    $lines = file($file);
    $data = array();
    foreach($lines as $line)
        $data[] = explode(';',trim($line));
    return $data;
}

// Simple table
function BasicTable($header, $data)
{
    // Header
    foreach($header as $col)
        $this->Cell(40,7,$col,1);
    $this->Ln();
    // Data
    foreach($data as $row)
    {
        foreach($row as $col)
            $this->Cell(40,6,$col,1);
        $this->Ln();
    }
}

// Better table
function ImprovedTable($header, $data)
{
    // Column widths
    $w = array(40, 35, 40, 45);
    // Header
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C');
    $this->Ln();
    // Data
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR');
        $this->Cell($w[1],6,$row[1],'LR');
        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
        $this->Ln();
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}


// Colored table
// $header: array header
// $w: array width of column
// $data: array two dimentions data.
function FancyTable($header,$w, $data)
{
    // Colors, line width and bold font
    $this->SetFillColor(255,0,0);
    $this->SetTextColor(255);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
    // Header
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
    $this->Ln();
    // Color and font restoration
    $this->SetFillColor(224,235,255);
    $this->SetTextColor(0);
    $this->SetFont('');
    // Data
    $fill = false;
    foreach($data as $row)
    {	
    	$i=0;
    	foreach($row as $key=>$property){
    		$width = ($w[$i]>0?$w[$i]:40);
        	$this->Cell($width,6,$property,'LR',0,'C',$fill);
    		$i++;
    	}
        $this->Ln();
        $fill = !$fill;
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}


//
// $data: thong tin mot con ngua
// {"name":"horseA","weight":"20","age":"2","fields":{"1":{"title":"Vaccinations","cost":"10","freq":"121","annual":"1440"},"2":{title // ":"Worming","cost":"23","freq":"19","annual":"20"}},"customfields":{"24":{"title":"cham soc","cost":"10","freq":"11","annual":"110"}// },"total":"2030"}
function HorseTable($data){
	$this->SetFillColor(255,255,255);
	$this->SetTextColor(121, 146, 64);
	$this->SetFont('Arial','',13);
	$this->Cell(130, 7, 'Horse Cost Calculator', 1, 0, 'C', true);
	$this->Ln();
	$this->Ln();
	$this->SetFont('Arial','',7);
	$this->Cell(130, 7, 'http://completehorsecare.com/', 0, 0, 'C', true);
	$this->Ln();
	// Colors, line width and bold font
    $this->SetFillColor(255,255,255);
    $this->SetTextColor(121, 146, 64);
    $this->SetDrawColor(225,225,225);
    $this->SetLineWidth(.3);
    $this->SetFont('Arial','',11);
    // horse info
    $this->Cell(30,5,'NAME',0,0,'L',true);
    $this->Cell(30,5,$data["name"],0,0,'L',true);
    $this->Ln();
    $this->Cell(30,5,'WEIGHT',0,0,'L',true);
    $this->Cell(30,5,$data["weight"],0,0,'L',true);
    $this->Ln();
    $this->Cell(30,5,'AGE',0,0,'L',true);
    $this->Cell(30,5,$data["age"],0,0,'L',true);
    $this->Ln();
    // cost info
    // table header
    $this->SetFillColor(201,201,148);
    $this->SetTextColor(0, 0, 0);
    $this->SetFont('Arial','B',9);
    $this->Cell(40,7,'ITEM',1,0,'C',true);
    $this->Cell(30,7,'COST',1,0,'C',true);
    $this->Cell(30,7,'FREQ',1,0,'C',true);
    $this->Cell(30,7,'ANNUAL COST',1,0,'C',true);
    $this->Ln();
    // table body
    
    $this->SetTextColor(0);
    $this->SetFont('');
    
    foreach($data["fields"] as $key=>$item){
    	$this->SetFillColor(224,235,255);
		$this->Cell(130,5,$key,1,0,'L',true);
		$this->Ln();
		foreach($item as $keyItem=>$field){
			$this->SetFillColor(209,218,235);		
	    	foreach($field as $keyField=>$value){
	    		
	    		if($keyField == "title"){
	    			$this->Cell(40,5,$value,1,0,'C',true);	
	    		} elseif ($keyField == "freq"){
	    			if($value=="52"){
	    				$value="Weekly";
	    			} elseif($value == "12"){
	    				$value="Monthly";
	    			} elseif($value == "4"){
	    				$value="Quarterly";
	    			} elseif($value == "1"){
	    				$value="Annually";
	    			} elseif($value == "8.7"){
	    				$value="6 Weekly";
	    			} elseif($value == "6.5"){
	    				$value="8 Weekly";
	    			} elseif($value == "2"){
	    				$value="6 Monthly";
	    			}
	    			$this->Cell(30,5,$value,1,0,'C',true);	
	    		} else {
	    			$this->Cell(30,5,$value,1,0,'C',true);	
	    		}
	    	}
	    	$this->Ln();
	    }
    }
    
    $this->Cell(100,7,"Annual Cost in Total:",1,0,'R',true);
	$this->Cell(30,7,$data["total"],1,0,'C',true);	
    $this->Ln(20);
}

// Colored table
// $header: array header
// $w: array width of column
// $data: array two dimentions data.
function FancyTableCol($header, $data,$w,$widthDefaul)
{
    // Colors, line width and bold font
    $this->SetFillColor(255,0,0);
    $this->SetTextColor(255);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
    // Header
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
    $this->Ln();
    // Color and font restoration
    $this->SetFillColor(224,235,255);
    $this->SetTextColor(0);
    $this->SetFont('');
    // Data
    $fill = false;
    $arrKey = array();
    foreach($data as $row)
    {	  	
    	foreach($row as $key=>$property){
    		if(!in_array($key, $arrKey)){
    			array_push($arrKey, $key);
    		}
    	}    
    }
 	
    foreach($arrKey as $key){
    	$i=0;
    	$width = isset($w[$i])?$w[$i]:$widthDefaul;
    	$this->Cell($width,6,$key,'LR',0,'C',$fill);
	    foreach($data as $row)
	    {
	    	$i++;
	    	$width = isset($w[$i])?$w[$i]:$widthDefaul;
	    	$value = isset($row[$key])?$row[$key]:"";
	    	$this->Cell($width,6,$value,'LR',0,'C',$fill);
	    }
	    $this->Ln();
        $fill = !$fill;
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}
}
// disable magicquotes
if (get_magic_quotes_gpc()) {
    function stripslashes_gpc(&$value)
    {
        $value = stripslashes($value);
    }
    array_walk_recursive($_REQUEST, 'stripslashes_gpc');
}
// end disable magic quotes

$strData = $_REQUEST["pdfData"];
$data = json_decode($strData, true);

$pdf = new PDF();
$pdf->Open();
$pdf->SetFont('Arial','',11);
$pdf->AddPage();
foreach($data as $key=>$horseData){
	$pdf->HorseTable($horseData);
}
$pdf->Output();
?>

